package org.zjx.action.game.inventor;

import org.zjx.action.Action;
import org.zjx.core.IWindow;
import org.zjx.entity.ParagraphBean;
import org.zjx.enums.FontType;
import org.zjx.enums.prop.SlotType;
import org.zjx.model.prop.ItemInstance;
import org.zjx.model.prop.ItemTemplate;
import org.zjx.model.prop.SlotItem;
import org.zjx.service.PropService;
import org.zjx.service.impl.PropServiceImpl;
import org.zjx.state.AccountState;
import org.zjx.state.ItemState;
import org.zjx.util.ImageUtil;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 同步物品道具
 */
public class MatchInventorAction extends Action {
    private final PropService propService = new PropServiceImpl();
    private final List<BufferedImage> itemImages;

    public MatchInventorAction(IWindow targetWindow, List<BufferedImage> itemImages) {
        super(targetWindow, false);
        this.itemImages = itemImages;
    }

    @Override
    protected void doExecute() throws Exception {
        List<String> imageStrings = convertImagesToStrings();
        List<ItemTemplate> itemTemplates = matchItemTemplates(imageStrings);
        List<SlotItem> slotItems = createSlotItems(itemImages, itemTemplates);
        List<ItemInstance> instances = syncSlotItems(slotItems);
        ItemState itemState = stateManager.getState(ItemState.class);
        itemState.getItems().put(SlotType.INVENTORY, instances);
    }

    /**
     * 转换图片为字符串
     */
    private List<String> convertImagesToStrings() {
        return itemImages.stream()
                .map(ImageUtil::bufferedImageToString)
                .collect(Collectors.toList());
    }


    /**
     * 匹配物品模板
     */
    private List<ItemTemplate> matchItemTemplates(List<String> imageStrings) {
        return propService.matcher(imageStrings);
    }

    /**
     * 创建槽位物品列表
     */
    private List<SlotItem> createSlotItems(List<BufferedImage> images, List<ItemTemplate> templates) {
        List<SlotItem> slotItems = new ArrayList<>();

        for (int i = 0; i < templates.size(); i++) {
            ItemTemplate template = templates.get(i);
            if (template == null) {
                slotItems.add(null);
                continue;
            }

            SlotItem slotItem = new SlotItem();
            slotItem.setName(template.getName());
            int finalI = i;
            List<ParagraphBean.Word> number = targetWindow.execute(op -> op.identifyFont(targetWindow, images.get(finalI), null, Collections.singletonList(new Color(255, 255, 255)), FontType.PROP, 0, false));
            if (number == null || number.isEmpty()) {
                slotItem.setStack(1);
            } else {
                slotItem.setStack(Integer.parseInt(number.get(0).getName()));
            }
            slotItems.add(slotItem);
        }

        return slotItems;
    }

    /**
     * 同步槽位物品
     */
    private List<ItemInstance> syncSlotItems(List<SlotItem> slotItems) {
        AccountState accountState = stateManager.getState(AccountState.class);
        return propService.syncSlotItems(accountState.getPlayerId(), slotItems, SlotType.INVENTORY);
    }
}
